package com.common.www.bean.entity.auth;

import org.springframework.boot.base.jpa.domain.annotations.QueryCacheable;
import org.springframework.boot.base.jpa.domain.entity.BaseEntity;
import com.common.www.utils.TxtUtil;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.cache.annotation.Cacheable;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Index;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@DynamicUpdate(false)
@DynamicInsert(false)
@Getter
@Setter
@Entity
@QueryCacheable
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "auth_user", indexes = {@Index(columnList = "name")})
public class AuthUser extends BaseEntity implements BaseUserInf {
    private static final long serialVersionUID = -1030211725004471486L;

    @JsonIgnoreProperties(value = {"authUsers"})
    @ManyToMany(fetch = FetchType.EAGER)
    @ApiModelProperty("角色（APP可忽略）")
    @JoinTable(name = "auth_user_ref_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
    private List<AuthRole> authRoles = new ArrayList<AuthRole>(0);

//    @JsonIgnoreProperties(value = {"children"})
//    @ManyToMany(fetch = FetchType.EAGER)
//    @ApiModelProperty("用户所属公司表")
//    @JoinTable(name = "auth_user_ref_company", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "company_id"))
//    private List<Company> company = new ArrayList<Company>(0);
    /**
     * 真实姓名
     */
    @Column(length = 64)
    private String name;
    /**
     * 用户名
     */
    @Column(length = 64)
    private String username;
    /**
     * 昵称
     */
    @Column(length = 64)
    private String nickName;
    /**
     * 身份号
     */
    @Column(length = 64)
    private String cardNum;


    /**
     * 年龄
     */
    @Column(length = 11)
    private Integer age;
    /**
     * desc
     */
    @Column(length = 512)
    private String des;
    /**
     * 邮箱
     */
    @Column(length = 64)
    private String email;
    /**
     * /**
     * 省市区
     */
    @Column(length = 64)
    private String province;
    /**
     * 省市区
     */
    @Column(length = 64)
    private String city;
    /**
     * 省市区
     */
    @Column(length = 64)
    private String region;
    /**
     * 七牛图片相对地址
     */
    @Column(length = 256)
    private String avatar;

    /**
     * 动态下载地址
     */
    @Transient
    public String avatarUrl;

    @Column(length = 255)
    private String password;

    @Column(length = 256)
    private String phone;
    /**
     * 状态  0.未提交 1.审核中 2.未通过审核 10.认证通过
     */
    @Column(length = 1)
    private Integer status;
    /**
     * 未通过备注
     */
    @Column(length = 256)
    private String remark;
    /**
     * 0.未知  1.男 2.女
     */
    @Column(length = 1)
    private Integer gender;

    @Column(nullable = false)
    private Boolean locked = false;

    /**
     * 极光设备id
     */
    @ApiModelProperty("jPushId")
    private String jPushId;
    /**
     * 小程序的openid(用户名、手机号、第三方平台都可以作为登录的凭证)
     */
    @Column(length = 256)
    private String openId;
    /**
     * app的openid(用户名、手机号、第三方平台都可以作为登录的凭证)
     */
    @Column(length = 256)
    private String appOpenId;
    /**
     * 联合公用id
     */
    @Column(length = 256)
    private String unionid;

    /**
     * 七牛图片相对地址
     */
    @Column(length = 512)
    private String idCardPathFront;
    /**
     * 七牛图片相对地址
     */
    @Column(length = 512)
    private String idCardPathReverse;
    /**
     * 相关关联的id，多个用逗号隔开
     */
    @Column(length = 512)
    private String storeIds;
    /**
     * 相关关联的id，多个用逗号隔开
     */
    @Column(length = 512)
    private String projectIds;

    /**
     * 用户存在标志
     * 可以逻辑删除
     */
    @Column(length = 1, columnDefinition = "tinyint default  1")
    private Integer flag;

    /**
     * 首先得到昵称 其次是 姓名 最后是 用户名
     */
    public String getNotNullName() {
        if (!TxtUtil.isEmpty(name)) {
            return name;
        } else if (!TxtUtil.isEmpty(username)) {
            return username;
        }
        return "用户暂无昵称";
    }

    /**
     * 首先性别名称
     */
    public String getGenderName() {
        if (gender == null) {
            return "未知";
        }
        switch (gender) {
            case 0:
                return "未知";
            case 1:
                return "男";
            case 2:
                return "女";
        }
        return "未知";
    }

    /**
     * 首先性别名称
     */
    public String getStatusName() {
        if (status == null) {
            return "未知";
        }
        switch (status) {
            case 0:
                return "未提交";
            case 1:
                return "审核中";
            case 2:
                return "未通过审核";
            case 10:
                return "认证通过";
        }
        return "未知";
    }

}
